package net.huashitong.user.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.user.entity.Department;
import net.huashitong.user.filter.DepartmentFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DepartmentDao extends StringDao<Department> {

    //查询条件
    public GenericQuery queryParam(GenericQuery query,DepartmentFilter filter){
        if (ObjectUtils.isNotBlank(filter.getDeptName())) {
            query.append(" and obj.deptName like:deptName").setParam("deptName",
                    StringUtils.getLikewords(filter.getDeptName()));
        }
        if (ObjectUtils.isNotBlank(filter.getDeptType())) {
            query.append(" and obj.deptType =:deptType").setParam("deptType",
                    filter.getDeptType());
        }
        if (ObjectUtils.isNotBlank(filter.getTypeName())) {
            query.append(" and obj.typeName =:typeName").setParam("typeName",
                    filter.getTypeName());
        }
        //模糊搜索
        if(StringUtils.isNotBlank(filter.getSearchWord())){
            query.append(" and (obj.deptName like:deptName or " +
                    "obj.typeName like:typeName) ").setParam("deptName",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("typeName",
                    StringUtils.getLikewords(filter.getSearchWord()));
        }
        query.append(" order by obj.deptType asc");
        return query;
    }

    public List<Department> queryByFilter(DepartmentFilter filter){
        GenericQuery query = createQuery("obj");
        query = queryParam(query,filter);
        return query.listResult(filter);
    }

    public List<Department> queryByFilterNoPage(DepartmentFilter filter){
        GenericQuery query = createQuery("obj");
        query = queryParam(query,filter);
        return query.listResult();
    }

    public List<Department> queryDeptNameExist(String deptName){
        GenericQuery query = createQuery("obj");
        query.append(" and obj.deptName =:deptName").setParam("deptName", deptName);
        return query.listResult();
    }



}
